package com.aerozhonghuan.oknet2;

import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 构建 get 方法时的 查询字符串，键值对形式。
 * Created by zhangyunfei on 17/6/23.
 */
//格式 :  http:// xxxx.com?p1=v1&p2=v2 ;

public class QueryParameterBuilder {
    private HashMap<String, String> data;
    private String mURL;

    public QueryParameterBuilder() {
        data = new HashMap<>();
    }

    public QueryParameterBuilder URL(String url) {
        if (TextUtils.isEmpty(url)) {
            throw new NullPointerException();
        }
//        if (mURL.startsWith("http://") {
//            throw new NullPointerException();
//        }
        mURL = url;
        return this;
    }

    public QueryParameterBuilder put(String key, String value) {
        data.put(key, value);
        return this;
    }

    public int size() {
        return data.size();
    }

    public String build() {
        StringBuilder sb = new StringBuilder(mURL);
        sb.append("?");
        int i = 0;
        for (Map.Entry<String, String> $i : data.entrySet()) {
            if (i++ != 0) {
                sb.append("&");
            }
            sb.append($i.getKey()).append("=").append($i.getValue());
        }
        return sb.toString();
    }
}
